package routinecontroller

import (
	"agent-list---web-development/list/common"
	"agent-list---web-development/list/model"
	"agent-list---web-development/list/response"
	"time"

	"net/http"

	"github.com/gin-gonic/gin"
)

func CrearteRoutine(c *gin.Context) {
	DB := common.GetDB()
	//获取参数
	title := c.PostForm("title")
	account := c.PostForm("account")
	//数据验证
	if len(account) != 7 {
		response.Response(c, http.StatusBadRequest, 422, nil, "用户账号格式错误！")
		return
	}
	user := model.User{}
	DB.Where("account=? ", account).First(&user)
	if user.Id == 0 {
		response.Response(c, http.StatusBadRequest, 422, nil, "该用户不存在！")
		return
	}
	routine := model.Routine{}
	DB.Where("useraccount=? and title=?", account, title).First(&routine)
	if routine.Id != 0 {
		response.Response(c, http.StatusUnprocessableEntity, 422, nil, "该打卡项目已存在！")
		return
	}
	stime, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
	//stime := time.Now()

	//创建打卡项，存到数据库
	var routine2 = model.Routine{
		Title:       title,
		Reconding:   make(model.Recondings, 31),
		USERAccount: account,
		STime:       stime,
	}
	DB.Create(&routine2)
	response.Success(c, gin.H{"routine": routine2}, "添加打卡项成功！")
}

//int, err := strconv.Atoi(string) 将字符串转换为int
